package com.pleso.model;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.pleso.utils.Utils;

public class Login {

	public JFrame displayLogin(final JFrame frame) {

		final JPanel loginPannel = new JPanel();

		frame.add(loginPannel);

		addLabels(loginPannel);

		final JTextField loginText = addTextBoxes(loginPannel);

		final JTextField passwordText = addTextBoxes(loginPannel);

		JButton submitButton = addSubmitButton(loginPannel);

		LoginAction(frame, loginPannel, loginText, passwordText, submitButton);
		return frame;

	}

	private void LoginAction(final JFrame frame, final JPanel loginPannel,
			final JTextField loginText, final JTextField passwordText,
			JButton submitButton) {
		submitButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (loginText.getText().contentEquals(Utils.getLogin())
						|| passwordText.getText().contentEquals(
								Utils.getPassword())) {
					new Dashboard(frame, loginText.getText());
					loginPannel.hide();
					frame.show();
				} else {
					JLabel message = new JLabel(
							"Login or password are incorrect");
					loginPannel.add(message);
					frame.show();
				}
				;
				frame.show();
			}
		});
	}

	private JButton addSubmitButton(final JPanel loginPannel) {
		JButton submitButton = new JButton("Submit");

		loginPannel.add(submitButton);
		return submitButton;
	}

	private JTextField addTextBoxes(final JPanel loginPannel) {
		final JTextField loginText = new JTextField();
		loginText.setPreferredSize(new Dimension(100, 20));
		loginPannel.add(loginText);
		return loginText;
	}

	private void addLabels(final JPanel loginPannel) {
		JLabel loginLabel = new JLabel("Login");
		JLabel passwordLabel = new JLabel("Password");
		loginPannel.add(loginLabel);
		loginPannel.add(passwordLabel);
	}

}